tempfile bin results2017

******************************************
*** 2017 data ****************************

**** putting together main decision form and exit survey ****
import excel raw_data/2017/exit_survey_transcription_4_10_17.xlsx, sheet("Sheet1") firstrow clear
rename SurveyID survey_number
save `results2017'

import excel raw_data/2017/full_results.xlsx, sheet("Sheet1") firstrow clear // this file is a newer copy & paste into Excel of the Transcriptions...docx produced directly after the workshop (before we used full_results_initial_transcription_wo_reasons.xlsx, which did not have the reasons)
rename (DecisionForm Lawthatshouldapply Reasoning) (survey_number law_chosen reasons)
merge 1:1 survey_number using `results2017'

list Q1 Q2 if _merge==2 // one observation: "I declined to participate" "Circuit" (dropped below). _merge == 3 are participants who both submitted a judgment and filled in the exit survey; _merge==1 did not fill in the survey (4 participants)
drop _merge

gen Forum=substr(Treatment,1,1)
gen Accident_state=substr(Treatment,2,1)
gen Sympathy=substr(Treatment,3,1)
drop Treatment

gen judgetype = Q2
replace judgetype = "District" if judgetype=="Magistrate" & F=="S" & A=="K" & S=="S" // randomization didn't assign SKS to any magistrate, but we accidentally treated one magistrate as district judge in assignment and want to do the same in our randomization tests; SKS is correct as evidenced by the judgment reasons for this participant (which mentions arguments that only occur in SD, and accident place in Kansas)

bys judgetype Forum Accident_state Sympathy: gen num = _n // for next step
save `results2017', replace

**** fixing missing judge type if possible, limiting set of possibilities for those where it can't be fixed ****
use raw_data/2017/randomization_result_anonymous_as_used_on_20170410.dta, clear // this is the actual assignment that we used in 2017, but with names (that we needed to distribute the paper materials to the right judges) removed
bys judgetype w_f w_a w_s: gen num = _n
rename (w_f w_a w_s) (Forum Accident_state Sympathy)
merge 1:1 judgetype Forum Accident_state Sympathy num using `results2017'
sort  Forum Accident_state Sympathy
list judgetype Forum Accident_state Sympathy survey if _merge==2 // 4 unmatched participants are all missing judge type. Treatments are: 1 SKN, 2 SKS, 1 WNN. Treatments recorded seem correct as judged by reasons.
list judgetype Forum Accident_state Sympathy		if _merge==1 // 7 unmatched assignments: --> 3 attrition plus 4 with missing info (?)
* comparison of the the foregoing lists shows:
	* a) participant with missing judge type in treatment SKN must be bankruptcy because only umatched SKN assignment is bankruptcy
replace judgetype = "Bankruptcy" if judgetype=="" & F=="S" & A=="K" & S=="N" & _merge==2
drop if judgetype=="Bankruptcy" & F=="S" & A=="K" & S=="N" & _merge==1
	* b) one participant with missing type in treatment SKS must be district because only unmatched SKS assignment is district. BUT there are too many SKS participants with missing type (2), since there is only one unmatched SKS assignment ... (I checked that the reasons of all SKS participants correspond to SK? treatment, with the partial exception of one judge who did not submit a decision [law_chosen=="N/A"])
	* c) participant with missing type in treatment WNN could be district or magistrate 
	* --> three participants (b and c) must remain "typeless" 

drop if _merge==1
drop _merge num
drop if mi(F) // that is the Circuit judge who did not submit a judgment (which would have listed the Forum) but did submit an exit survey

save `results2017', replace


******************************************
*** 2018 data ****************************

import excel raw_data/2018/results_2018_online.xlsx, sheet("Sheet1") firstrow allstring clear
save `bin'

import excel raw_data/2018/results_2018_paper.xlsx, sheet("Sheet1") firstrow clear
append using `bin', gen(online)

rename (jurisdiction accident_state sympathy lawchosen proportion type_of_judge ever_decided Realistic what_was_study_about heard_about_prior_study) ///
	(Forum Accident_state Sympathy law_chosen Q1_proportion_other_judges_perce judgetype Q3_hadtochoose  Q4_realistic  Q5_whatabout   Q6_prior_knowledge)
drop if mi(F) & mi(A) & mi(S) // this catches empty lines
replace judgetype = "Circuit" if regexm(judgetype,"Circuit but was district for 8 years")
replace judgetype = "Bankruptcy" if judgetype=="District or Bankruptcy?" // one judge's circle included both bankruptcy and district (and a bit of "federal [claims]"), but was most likely bankruptcy because the circle began on the very right of "bankruptcy" before swinging far left to include also "district"
drop if regexm(note,"I did this online as well") // one 2018 judge submitted on paper and online; online has long reasons
assert note==""
drop note

save `bin', replace

append using `results2017', gen(year)
recode year (0 = 2018) (1 = 2017)


******************************************
**** cleanup *****************************
recode online (. = -1) // 2017 had missing values here, which would prevent use as factor variable
replace S = trim(S)
replace judgetype = "Bankruptcy" if judgetype=="Bankruptcy or Federal Claims"

drop if inlist(law_chosen,"","N/A")
replace law_chosen = "K" if inlist(law_chosen,"Kansas","Kansas (cap on noneconomic damages)")
replace law_chosen = "N" if inlist(law_chosen,"Nebraska","Nebraska (no cap on noneconomic damages)")
assert inlist(law_chosen,"K","N")

replace Q1 = "100" if Q1=="All"
replace Q1 = "33"  if Q1=="1/3"
replace Q1 = "50"  if Q1=="50-50"
destring Q1, gen(certainty) force
replace certainty = certainty*100 if certainty<1 & certainty>=0

**** adding convenience variables
gen lexloci = A==law_chosen
gen cap = law_chosen=="K"
gen correct = ((F=="W" & A==law_) | (F=="S" & A!=law_))
gen prior_knowledge=Q6=="Yes"

**** memory management (this matters for large number of I/O operations in rerandomization & inference)
run labels.do
encode judgetype, gen(judge) label(judge)
encode F, gen(forum) label(forum)
encode A, gen(a_location) label(location)
encode S, gen(sympathy) label(sympathy)
drop judgetype F A S
rename judge judgetype
compress

drop Q1 Q2
order year online survey forum a_location sympathy law_chosen lexloci cap correct reasons certainty Q3 Q4 Whynot Q5 Q6

save alldata.dta, replace
